<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\BaseController;
use App\Jobs\RandStarJob;
use App\Jobs\SearchJob;
use App\Models\Article;
use App\Models\ArticleRelateCircle;
use App\Models\Circle;
use App\Models\City;
use App\Models\Comment;
use App\Models\Focus;
use App\Models\Star;
use App\Models\User;
use App\Models\UserJoinCircle;
use App\Models\UserNotInterest;
use App\Models\UserNotInterestTag;
use App\Models\UserRead;
use App\Models\UserReadLog;
use App\Mongo\ArticleMon;
use App\Services\BalanceService;
use App\Services\ReadStoreService;
use App\Services\SafeCheckService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;


class ArticleController extends BaseController
{
    public function index(Request $request, $action = null, $is_raw_return = false) {
        $title = $request->input('title');
        $order_by_key = $request->input('order_by_key', 'articles.id');
        $order_by_value = $request->input('order_by_value', 'desc');
        $circle_id = $request->input('circle_id');
        $action = $action ?: $request->input('action');
        $login_user_id = $this->getUserId();
        $user_id = $request->input('user_id', $login_user_id);
        $arr_user_id = $arr_id = [];
        $list = new Article();
        $is_order_by_ids = false;
        $per_page = per_page();
        $circle_query = $is_star_query = $comment_query = true;
        $arr_query = false;
        $read_article_ids = $request->input('read_article_ids');
        if ($read_article_ids) {
            $user_id = $user_id ?: $request->header('fake_uid');
            ReadStoreService::handle($user_id, $read_article_ids, $login_user_id);
        }

        if ($action == 'circle_index_hot' || $action == 'circle_index_latest' || $action == 'activity_rank') {
            $this->validate($request, [
                'circle_id' => 'required'
            ]);
            $list = $this->_searchByCircleId([$circle_id]);
            if ($action == 'circle_index_hot') {
                $list = $list->orderBy('weight', 'desc');
            } else if ($action == 'circle_index_latest') {
                $list = $list->orderBy('sort', 'desc')->orderBy('latest_comment_at', 'desc');
            } else {
                $list = $list->orderBy('activity_star_num', 'desc')->orderBy('weight', 'desc');
            }

        } else if ($action == 'activity_index_hot' || $action == 'activity_index_latest') {//暂时没用 都上 circle_xxx
            $this->validate($request, [
                'circle_id' => 'required'
            ]);
            $list = $this->_searchByCircleId([$circle_id]);
            if ($action == 'circle_index_hot') {
                $list = $list->orderBy('activity_star_num', 'desc');
            } else {
                $list = $list->orderBy('sort', 'desc')->orderBy('latest_comment_at', 'desc');
            }
        } else if($action == 'my_circle') {
            $join_list = UserJoinCircle::where('user_id', $user_id)->get()->toArray();;
            $arr_circle_id = array_column($join_list, 'circle_id');
            $list = $this->_searchByCircleId($arr_circle_id);
        } else if($action == 'search') {
//            $sql = '(SELECT id,weight FROM articles WHERE MATCH (title) AGAINST ("' . $title . '")) as ma';
//            $temp = \DB::table(\DB::raw($sql))->orderBy('weight', 'desc')->paginate(per_page());
//            $arr_id = array_column($temp->items(), 'id');
            $circle_query = false;
//            $arr_query = true;
            $list = $list->where('title', 'like', '%' . $title . '%')->orderBy('weight', 'desc');
            $this->dispatch(new SearchJob($user_id, $title));
        } else if($action == 'nearby') {
            $this->validate($request, [
                'longitude' => 'required',
                'latitude' => 'required'
            ]);
            $input = $request->input();
//            $cache_key = substr(md5($input['longitude'] . $input['latitude']), 8, -8);
//            $city = Cache::remember($cache_key, 60, function () use($input){
//                return reverse_geocode($input['longitude'], $input['latitude']);
//            });
//            $city_code = City::get_code_by_name($city);
            $user_id = $user_id ?: $request->header('fake_uid');
//            $arr_read = UserRead::getReadIds($user_id);

            $city = $request->input('city');
            $article_list = Article::where('status', Article::STATUS_NORMAL)->where('city', $city)->orderBy('id', 'desc')
                ->select('id')->paginate(per_page());
            $article_items = $article_list->items();
            if (count($article_items) == 0) {
                $article_list = ArticleMon::where('location', 'near', [
                    '$geometry' => [
                        'type' => 'Point',
                        'coordinates' => [
                            floatval($input['longitude']),
                            floatval($input['latitude'])
                        ],
                    ],
//                '$maxDistance' => 100000,
                ])->where('status', Article::STATUS_NORMAL)->select('id')->paginate(per_page());
//            ->where('city_code', $city_code)
                $article_items = $article_list->items();
            }

            $arr_id = array_column($article_items, 'id');
//            $left_num = $per_page - count($arr_id);
            $is_order_by_ids = true;
//            if ($left_num) {
//                $left_article_list = Article::where('city', $city)->whereNotIn('id', $arr_id)->inRandomOrder()->take($left_num)->get()->toArray();
//                $arr_id = array_merge($arr_id, array_column($left_article_list, 'id'));
//            }
            $arr_query = true;
            $request->merge(['page' => 1]);
        } else if($action == 'focus_users') {
            if (!$user_id)
                abort(401);
            $focus_list = Focus::where('user_id', $user_id)->get()->toArray();
            $arr_user_id = array_column($focus_list, 'target_user_id');
//            if ($user_id == $login_user_id)
//                array_push($arr_user_id, $user_id);
            $list = $list->whereIn('user_id', $arr_user_id);
        } else if($action == 'rec') {
            $circle_query = $comment_query = false;
            $user_id = $user_id ?: $request->header('fake_uid');
            $arr_tag_id = UserNotInterestTag::where('user_id', $user_id)->get()->pluck('tag_id')->all();
            $arr_read = UserRead::getReadIds($user_id);
            $arr_id = Article::whereNotIn('id', $arr_read)->where('is_special_choice', 1)->when(!empty($arr_tag_id), function ($q) use($arr_tag_id) {
                $q->whereDoesntHave('article_tag', function ($qr) use($arr_tag_id) {
                    $qr->whereIn('article_tag_id', $arr_tag_id);
                });
            })->where('status', Article::STATUS_NORMAL)->orderby('weight', 'desc')->orderBy('id', 'desc')
                ->take($per_page)->get()->pluck('id')->toArray();
            $left_num = $per_page - count($arr_id);
            if ($left_num) {
                $is_order_by_ids = true;
                $left_article_list = Article::whereNotIn('id', $arr_id)->when(!empty($arr_tag_id), function ($q) use($arr_tag_id) {
                    $q->whereDoesntHave('article_tag', function ($qr) use($arr_tag_id) {
                        $qr->whereIn('article_tag_id', $arr_tag_id);
                    });
                })->where('status', Article::STATUS_NORMAL)->inRandomOrder()->take($left_num)->get()->toArray();
                $arr_id = array_merge($arr_id, array_column($left_article_list, 'id'));
            }
            $arr_query = true;
        } else if($action == 'album') {
            $circle_query = $is_star_query = $comment_query = false;
            $list = $list->where('is_join_album', 1)->where('user_id', $user_id)->where('file_path', '!=', '[]');
        } else if($action == 'my_publish') {
            $circle_query = $is_star_query = $comment_query = false;
            $list = $list->where('user_id', $user_id);
        } else if($action == 'my_star') {
            $circle_query = $is_star_query = $comment_query = false;
            $list = Article::select('articles.*')->join('stars', function ($join) use($user_id) {
                $join->on('articles.id', 'stars.entity_id')->where('stars.type', Star::TYPE_ARTICLE)->where('stars.user_id', $user_id);
            })->orderBy('stars.id', 'desc');
        } else if($action == 'detail_relate') { //内容详情关联
            $arr_query = true;
            $request->merge(['page' => 1]);
            $user_id = $user_id ?: $request->header('fake_uid');
            $arr_read = UserRead::getReadIds($user_id);
            $this->validate($request, [
                'id' => 'required',
                'tag_ids' => 'present',
                'type' => 'required',
            ]);
            $type = $request->input('type');
            $id = $request->input('id');
            $tag_ids = $request->input('tag_ids');

            if (!$tag_ids) {
                $arr = [
                    'list' => [],
                    'total' => 0,
                    'has_more_page' => false,
                    'extra' => null
                ];
                return $this->success($arr);
            }

            array_push($arr_read, $id);
            //not interest
            $arr_not_interest_tag_id = UserNotInterestTag::where('user_id', $user_id)->get()->pluck('tag_id')->all();
            $arr_id = Article::whereNotIn('id', $arr_read)->when(!empty($arr_not_interest_tag_id), function ($q) use($arr_not_interest_tag_id) {
                $q->whereDoesntHave('article_tag', function ($qr) use($arr_not_interest_tag_id) {
                    $qr->whereIn('article_tag_id', $arr_not_interest_tag_id);
                });
            })->where('is_special_choice', 1)->when($type == Article::TYPE_VIDEO, function ($q) {
                $q->where('type', Article::TYPE_VIDEO);
            })->where('tag_ids', $tag_ids)->orderBy('id', 'desc')
                ->take($per_page)->get()->pluck('id')->toArray();
            $left_num = $per_page - count($arr_id);

            $arr_tag_id = explode(",", substr($tag_ids, 1,-1));
            if ($left_num) {
                $arr_read = array_merge($arr_read, $arr_id);
                $is_order_by_ids = true;
                //第二次精选库-局匹配
                $left_article_list = Article::whereNotIn('id', $arr_read)->when(!empty($arr_not_interest_tag_id), function ($q) use($arr_not_interest_tag_id) {
                    $q->whereDoesntHave('article_tag', function ($qr) use($arr_not_interest_tag_id) {
                        $qr->whereIn('article_tag_id', $arr_not_interest_tag_id);
                    });
                })->whereHas('article_tag', function ($qr) use($arr_tag_id){
                    $qr->whereIn('article_tag_id', $arr_tag_id);
                })->where('is_special_choice', 1)->when($type == Article::TYPE_VIDEO, function ($q) {
                    $q->where('type', Article::TYPE_VIDEO);
                })->orderBy('id', 'desc')
                    ->take($per_page)->get()->pluck('id')->toArray();
                $arr_id = array_merge($arr_id, $left_article_list);


                $left_num = $per_page - count($arr_id);
                if ($left_num) {
                    $arr_read = array_merge($arr_read, $arr_id);
                    //第三次非精选库-全局匹配
                    $left_article_list = Article::whereNotIn('id', $arr_read)->when(!empty($arr_not_interest_tag_id), function ($q) use($arr_not_interest_tag_id) {
                        $q->whereDoesntHave('article_tag', function ($qr) use($arr_not_interest_tag_id) {
                            $qr->whereIn('article_tag_id', $arr_not_interest_tag_id);
                        });
                    })->where('is_special_choice', 0)->when($type == Article::TYPE_VIDEO, function ($q) {
                        $q->where('type', Article::TYPE_VIDEO);
                    })->where('tag_ids', $tag_ids)
                        ->orderBy('id', 'desc')
                        ->take($per_page)->get()->pluck('id')->toArray();

                    $arr_id = array_merge($arr_id, $left_article_list);
                    $left_num = $per_page - count($arr_id);
                    if ($left_num) {
                        $arr_read = array_merge($arr_read, $arr_id);
                        //第四非精选库-局匹配
                        $left_article_list = Article::whereNotIn('id', $arr_read)->when(!empty($arr_not_interest_tag_id), function ($q) use($arr_not_interest_tag_id) {
                            $q->whereDoesntHave('article_tag', function ($qr) use($arr_not_interest_tag_id) {
                                $qr->whereIn('article_tag_id', $arr_not_interest_tag_id);
                            });
                        })->whereHas('article_tag', function ($qr) use($arr_tag_id){
                            $qr->whereIn('article_tag_id', $arr_tag_id);
                        })->where('is_special_choice', 0)
                            ->when($type == Article::TYPE_VIDEO, function ($q) {
                                $q->where('type', Article::TYPE_VIDEO);
                            })->orderBy('id', 'desc')
                            ->take($per_page)->get()->pluck('id')->toArray();
                        $arr_id = array_merge($arr_id, $left_article_list);
                    }
                }
            }
        }
        $list = $list->where('status', Article::STATUS_NORMAL)->when($arr_query, function ($q) use($arr_id, $is_order_by_ids){
            $q->whereIn('id', $arr_id);
            if ($is_order_by_ids) {
                $q->orderBy(\DB::raw('FIND_IN_SET(id, "' . implode(",", $arr_id) . '"' . ")"));
            }
        })->with(['user' => function ($q) use($user_id){
                $q->select('id', 'nickname', 'weixin', 'avatar_url')->withCount(['is_focus' => function($qr) use($user_id) {
                    $qr->where('focus.user_id', $user_id);
                }]);
            }, 'user.animal' => function ($q) {
                $q->select('user_id', 'title', 'birthday', 'category_title');
        }])->when($is_star_query, function ($q) use($user_id){
            $q->withCount(['is_star' => function ($qr) use ($user_id) {
                $qr->where('stars.user_id', $user_id)->where('type', Star::TYPE_ARTICLE);
            }]);
        })->when($circle_query, function ($q) {
            $q->with(['circles' => function($qt) {
                $qt->select('title', 'circles.id', 'is_activity', 'join_count');
            }]);
        })->when(!$is_order_by_ids, function ($q) use($order_by_key, $order_by_value, $arr_id){
            $q->orderBy($order_by_key, $order_by_value);
        })->paginate(per_page());

        foreach ($list as $item) {
            if ($comment_query) {
                $item->comment = Comment::cacheFor(3600)->cacheTags(['article:' . $item->id. '_' . $item->latest_comment_at])->where('entity_id', $item->id)
                    ->select('id', 'user_id', 'message', 'entity_id')
                    ->with(['user' => function($qr) {
                        $qr->select('id', 'nickname');
                    }])->where('parent_id', 0)->orderBy("star_num", "desc")->orderBy("id", "desc")->limit(2)->get();
            }
        }
        if ($is_raw_return)
            return $list;

        $total = $has_more_page = false;
        if($action == 'nearby') {
            $total = $article_list->total();
            $has_more_page = $article_list->hasMorePages();
        }
        return $this->paginate($list, null, $total, $has_more_page);
    }

    private function _searchByCircleId($circle_id = []) {
        return Article::join('article_relate_circles', function ($join) use($circle_id){
            $join->on('articles.id', 'article_relate_circles.article_id')
                ->whereIn('circle_id', $circle_id);
        })->groupBy('articles.id');
    }

    public function rec(Request $request) {
        $new_item = [];

        $user_id = $request->header('fake_uid', 'zero');
        $cache_key = 'rec_activity_' . $user_id;
        if (!Cache::get($cache_key)) {
            $request->merge(['per_page' => 1, 'page' => 1, 'is_activity' => 1]);
            $ctrl_activity = new CircleController();
            $activity_List = $ctrl_activity->index($request, null, true);
            foreach ($activity_List as $item) {
                $item->media_type = 'circle';
            }
            if (!empty($activity_List[0])) {
                $activity_List[0]['logo'] = 'https://qiniu.ling7.net/FqyaPjpug4tqOwGO6hDc_jbdk-sB';
                array_push($new_item, $activity_List[0]);
            }
            Cache::set($cache_key, 1, 7200);
        }

        $request->merge(['per_page' => 2, 'page' => 1, 'is_activity' => 0]);
        $ctrl = new CircleController();
        $circleList = $ctrl->index($request, 'rand_rec', true);

        foreach ($circleList as $item) {
            $item->media_type = 'circle';
        }

        $request->merge(['per_page' => 10, 'page' => 1]);
        $articleList = $this->index($request, 'rec', true);
        $articleList_total = $articleList->total();
        $key1 = rand(0, $articleList_total-1);
        for ($i = 0; $i <= 10; $i++) {
            $key2 = rand(0, $articleList_total-1);
            if ($key2 == $key1)
                continue;
            else if ($key2 + 1 == $key1)
                continue;
            else if ($key2 -1 == $key1)
                continue;
            else
                break;
        }

        foreach ($articleList as $key => $item) {
            $item->media_type = 'article';
            $new_item[] = $item;
            if ($key == $key1) {

                $new_item[] = $circleList[0];
            }
            if ($key == $key2) {

                $new_item[] = $circleList[1];
            }
        }
        $ret = [
            'list' => $new_item,
            'total' => $articleList_total + 2,
            'has_more_page' => true,
            'extra' => null
        ];;
        return $this->success($ret);
    }

    public function info(Request $request)
    {
        $this->validate($request, [
            'id' => 'required',
        ]);
        $id = \request('id');
        $user_id = $this->getUserId();
        $info = Article::with(['comment' => function($qr) {
            $qr->select('id', 'user_id', 'message', 'entity_id')->where('parent_id', 0)->orderBy("star_num", "desc")->orderBy("id", "desc")->limit(2);
        }, 'comment.user' => function($qr) {
            $qr->select('id', 'nickname');
        }])->withCount(['is_star' => function ($qr) use ($user_id) {
            $qr->where('user_id', $user_id)->where('type', Star::TYPE_ARTICLE);
        }])->with(['circles' => function($qt) {
            $qt->select('title', 'circles.id', 'join_count', 'is_activity', 'activity_start_at', 'activity_end_at');
        }])->with(['user' => function ($q) use($user_id){
            $q->select('id', 'nickname', 'weixin', 'avatar_url')->withCount(['is_focus' => function($qr) use($user_id) {
                $qr->where('focus.user_id', $user_id);
            }]);
        }, 'user.animal' => function ($q) {
            $q->select('user_id', 'title', 'birthday', 'category_title');
        }])->find($id);
        if (!$info || $info->status == Article::STATUS_DELETE)
            return $this->failed('动态不存在');

        Article::where('id', $id)->increment('pv', 1);
        $circles = $info->circles;
        $info->activity_status = null;
        if ($circles) {
            foreach ($circles as $circle) {
                if ($circle->is_activity) {
                    $info->activity_status = $circle->activity_status;
                    break;
                }
            }
        }

        return $this->success($info);
    }

    public function store(Request $request, $param = null)
    {
        $this->validate($request, [
//            'title' => 'required',
//            'type' => 'required',
//            'longitude' => 'required',
//            'latitude' => 'required'
        ]);


        $input = $param ?: $request->input();
        $user = $this->getUser();
        $input['user_id'] = $user_id = $user->id;

        $circle_ids = $input['circle_ids'] ?? null;//$request->input('circle_ids');
        if ($circle_ids)
            $arr_circle_id = explode(",", $circle_ids);
        else
            $arr_circle_id = [];

        if (!empty($input['title'])) {
            $hit = SafeCheckService::handle(['type' => 'text', 'content' => $input['title']]);
            if ($hit)
                return $this->failed('发布内容包含敏感词');
        }

        $id = \request("id");
        $activity_id = $request->input('activity_id');
        if ($activity_id) {
            array_push($arr_circle_id, $activity_id);
            if ($activity_id == 3113) {
                array_push($arr_circle_id, 3112);
            }
        }

        $city_code = $city = '';
        if (isset($input['longitude']) && isset($input['latitude'])) {
            $city = get_city_by_position($input);//reverse_geocode($input['longitude'], $input['latitude']);
            if (!$city)
                return $this->failed('经纬度获取城市失败');
            $city_code = City::get_code_by_name($city);
            if (!$city_code) {
                \Log::error('cities表该城市不存在:' . $city);
                return $this->failed('该城市不支持');
            }
            $input['city_code'] = $city_code;
            $input['city'] = $city;
        }

        $input['activity_id'] = $activity_id;
        $input['latest_comment_at'] = time();
        return \DB::transaction(function () use($id, $input, $city_code, $city, $arr_circle_id, $activity_id, $user_id, $user, $param) {
            if ($id) {
                $article =  Article::find($id);
                $arr_old_article_id = $article->circles()->get(['circles.id'])->pluck('id')->all();
                $should_delete_arr = array_diff($arr_old_article_id, $arr_circle_id);
                $should_insert_arr = array_diff($arr_circle_id, $arr_old_article_id);
                Circle::whereIn('id', $should_delete_arr)->increment('feed_count', -1);
                Circle::whereIn('id', $should_insert_arr)->increment('feed_count', 1);

                $article->update($input);

                ArticleMon::where('id', $id)->update([
                    'city' => $city,
                    'location' => [
                        'type' => 'Point',
                        'status' => Article::STATUS_NORMAL,
                        'coordinates' => [
                            floatval($input['longitude']),
                            floatval($input['latitude'])
                        ]
                    ],
                    'city_code' => $city_code
                ]);

            } else {
                $input['weight'] = 3;
                $article = Article::create($input);
                $id = $article->id;

                if ($city_code) {
                    ArticleMon::create([
                        'id' => intval($id),
                        'city' => $city,
                        'status' => Article::STATUS_NORMAL,
                        'location' => [
                            'type' => 'Point',
                            'coordinates' => [
                                floatval($input['longitude']),
                                floatval($input['latitude'])
                            ]
                        ],
                        'city_code' => $city_code
                    ]);
                }
                Circle::whereIn('id', $arr_circle_id)->increment('feed_count', 1);

                $rand_minute = rand(30, 180);
                RandStarJob::dispatch($id)->delay(now()->addMinutes($rand_minute));
            }
//            if ($activity_id) {
//                $exists = UserJoinCircle::where('user_id', $user_id)->where('circle_id', $activity_id)->where('is_activity', 1)->exists();
//                if (!$exists) {
//                    UserJoinCircle::create([
//                        'user_id' => $user_id,
//                        'circle_id' => $activity_id,
//                        'is_activity' => 1
//                    ]);
//                    Circle::where('id', $activity_id)->increment('join_count', 1);
//                }
//            }
            $new_arr = [];
            foreach ($arr_circle_id as $val) {
                $new_arr[$val] = [
                    'article_user_id' => $article->user_id
                ];
            }
            $article->circles()->sync($new_arr);
            //同时设置关注圈子
            $circle_count = 0;
            foreach ($arr_circle_id as $circle_id) {
                $info = UserJoinCircle::where('user_id', $user_id)->where('circle_id', $circle_id)->first();
                if (!$info) {
                    UserJoinCircle::create([
                        'user_id' => $user_id,
                        'circle_id' => $circle_id,
                        'is_activity' => $activity_id ? 1 : 0
                    ]);
                    Circle::where('id', $circle_id)->increment('join_count', 1);
                    $circle_count += 1;
                    \Cache::forget(Circle::CACHE_CIRCLE_COVER_USER . "_" . $circle_id);
                }
            }

            $user->article_count += 1;
            $user->circle_count += $circle_count;
            $user->save();

            if (is_null($param))
                BalanceService::article($user->id);

            return $this->success(['id' => $id]);
        });
    }

    public function readStore(Request $request) {
        $is_login = $user_id = $this->getUserId();
        $user_id = $user_id ?: $request->header('fake_uid');
        if (!$user_id)
            return $this->failed('缺少参数-fake_uid');

        $article_ids = $request->input('article_ids');
        $arr_article_id = explode(",", $article_ids);
        $model = UserRead::where('uid', $user_id)->first();
        if (!$model) {
            $model = new UserRead();
            $content = '';
        } else {
            $content = $model->content;
        }
        Article::whereIn('id', $arr_article_id)->increment('pv', 1);
        $log = new UserReadLog(['user_id' => $user_id]);
        foreach ($arr_article_id as $key => $id) {
            if (strpos($content, "," . $id . ",") !== false) {
                unset($arr_article_id[$key]);
            }
            if ($is_login) {
                $log->create([
                    'user_id' => $user_id,
                    'article_id' => $id
                ]);
            }
        }
        if (!empty($arr_article_id))
            $content .= "," . implode(",", $arr_article_id) . ",";

        $model->content = $content;
        $model->uid = $user_id;
        $model->save();

        return $this->success([]);
    }


    public function destroy(Request $request)
    {
        $user = $this->getUser();
        $user_id = $user->id;
        $this->validate($request, [
            'id' => 'required',
        ]);
        $id = $request->input('id');
        $article = Article::find($id);
        if (!$article)
            return $this->failed('不存在');

        if ($user_id != $article->user_id)
            return $this->failed('无权删除');

        try {
            return \DB::transaction(function () use($article, $id, $user) {
                $arr_old_article_id = $article->circles()->get(['circles.id'])->pluck('id');
                Circle::whereIn('id', $arr_old_article_id)->increment('feed_count', -1);
                $user->article_count -= 1;
                if ($user->article_count >= 0)
                $user->save();

                ArticleRelateCircle::where('article_id', $article->id)->delete();
                //动态点赞删除
                Star::where('type', Star::TYPE_ARTICLE)->where('entity_id', $article->id)->delete();
                //评论删除
                $arr_comment_id = Comment::where('entity_id', $article->id)->pluck('id')->all();
                Comment::whereIn('id', $arr_comment_id)->delete();
                //评论的点赞删除
                Star::where('type', Star::TYPE_COMMENT)->whereIn('entity_id', $arr_comment_id)->delete();

                ArticleMon::where('id', $article->id)->delete();
                $article->delete();
                return $this->success(['id' => $id]);
            });
        } catch (\Exception $e) {
            \Log::error($e);
            return $this->failed($e->getMessage());
        }
    }

    public function notInterest(Request $request) {
        $this->validate($request, [
            'id' => 'required',
        ]);
        $id = $request->input('id');
        $article = Article::with('article_tag')->find($id);
        if (!$article)
            return $this->failed('不存在');

        $user = $this->getUser();
        $exist = UserNotInterest::where('user_id', $user->id)->where('article_id', $id)->first();
        if ($exist)
            return $this->success([]);

        $article_tags = $article->article_tag;
        $arr_article_tag = $article_tags->pluck('id')->all();
        foreach ($arr_article_tag as $key => $id) {
            if ($id >5 )
                unset($arr_article_tag[$key]);
        }
        if (!empty($arr_article_tag)) {
            $user->not_interest_article_tag()->detach($arr_article_tag);
            $user->not_interest_article_tag()->attach($arr_article_tag);
            UserNotInterestTag::flushQueryCache();
        }
        UserNotInterest::create([
            'user_id' => $user->id,
            'article_id' => $id
        ]);
        $user->increment('not_interest_count');
        return $this->success([]);
    }

}

